package com.cat.slidingWindow;

import java.util.Arrays;

/**
 * @author cat
 * @description https://leetcode.cn/problems/sliding-subarray-beauty/
 * @create 2025/7/28 10:22
 * @since JDK17
 */

public class Solution05 {
    static int[] cnt = new int[101];

    public int[] getSubarrayBeauty(int[] nums, int k, int x) {
        Arrays.fill(cnt, 0);
        int n = nums.length, m = 50;
        int[] ans = new int[n - k + 1];
        for (int i = 0; i < k - 1; i++) {
            cnt[nums[i] + m]++;
        }
        for (int l = 0, r = k - 1, j, i; r < n; r++, l++) {
            cnt[nums[r] + m]++;
            j = x;
            for (i = 0; i <= 100 && j > 0; i++) {
                j -= cnt[i];
            }
            if (i >= m) {  // 正数
                ans[l] = 0;
            } else {
                ans[l] = i - m;
            }
            cnt[nums[l] + m]--;
        }
        return ans;
    }
}
